package com.springbootdemo.demo.lucene;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LuceneDemo {

    // 定义索引库的位置
    private static final String INDEX_PATH = "F:\\lucene-index";

    public static void main(String[] args) throws Exception {
        LuceneDemo luceneDemo = new LuceneDemo();

        //luceneDemo.createIndex();
        luceneDemo.searchIndex();
    }



    /**
     * 生成索引到索引库
     * @throws IOException
     */
    private void createIndex() throws IOException {
        //document封装数据
        Document document = new Document();
        StringField id = new StringField("id", "1", Field.Store.YES);
        TextField content = new TextField("content", "好好学习lucene", Field.Store.YES);
        document.add(id);
        document.add(content);

        //创建存放索引库的目录
        FSDirectory directory = FSDirectory.open(new File(INDEX_PATH));

        //创建分词器
        IKAnalyzer ikAnalyzer = new IKAnalyzer();

        //创建索引写入器配置
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, ikAnalyzer);

        //创建写入器
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        List<Document> documentList = new ArrayList<>();
        documentList.add(document);

        indexWriter.addDocuments(documentList);

        indexWriter.commit();

        indexWriter.close();
    }

    /**
     * 查询索引
     */
    private void searchIndex() throws Exception {

        //多字段查询解析器
        //MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(Version.LATEST, new String[]{"id", "content"}, new IKAnalyzer());
        //对搜索的参数进行解析(同时去匹配id和content)
        //Query query = multiFieldQueryParser.parse("");

        Term term = new Term("content", "好");
        FuzzyQuery query = new FuzzyQuery(term, 2);

        //获取索引库对象
        DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(new File(INDEX_PATH)));

        //获取查询对象
        IndexSearcher indexSearcher = new IndexSearcher(directoryReader);

        //传入查询条件查询
        TopDocs topDocs = indexSearcher.search(query, Integer.MAX_VALUE);

        //取出命中的文档
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;

        for(ScoreDoc sd : scoreDocs){
            Integer docID = sd.doc;
            Document document = indexSearcher.doc(docID);
            System.out.println(sd.score);
            System.out.println("搜索到的结果集id = " + document.get("id"));
            System.out.println("搜索到的结果集content = " + document.get("content"));
        }
        indexSearcher.getIndexReader().close();
    }
}
